package com.yswee.mallnavi.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

public class ProportionalImageView extends AppCompatImageView {

    public ProportionalImageView(Context context) {
        super(context);
    }

    public ProportionalImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProportionalImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        if (width > 0) {
            if (getDrawable() instanceof BitmapDrawable) {
                BitmapDrawable drawable = (BitmapDrawable) getDrawable();
                if (drawable != null) {
                    int height = (int) (width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth());
                    setMeasuredDimension(width, height);
                }
            }
        }
    }
}